import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { AiTrainingDatasetSamplesApi } from '#/api/ai/dataset/trainingDatasetSamples';
import type { DescriptionItemSchema } from '#/components/description';

import { h } from 'vue';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { Rate, Typography } from 'ant-design-vue';

import { getVersionNumList } from '#/api/ai/dataset/trainingDatasetVersions';
import { DictBadge } from '#/components/dict-badge';
import DictTag from '#/components/dict-tag/dict-tag.vue';

const { Paragraph } = Typography;
/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'versionId',
      label: '数据集版本id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'datasetId',
      label: '数据集id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'rawContent',
      label: '数据集内容',
      rules: 'required',
      component: 'Textarea',
      componentProps: {
        rows: 6,
        placeholder: '请输入数据集内容',
      },
    },
    {
      fieldName: 'versionNum',
      label: '版本号',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入版本号',
      },
    },
    {
      fieldName: 'isValid',
      label: '是否有效',
      rules: 'required',
      component: 'RadioGroup',
      defaultValue: true,
      componentProps: {
        options: [
          {
            label: '是',
            value: true,
          },
          {
            label: '否',
            value: false,
          },
        ],
        buttonStyle: 'solid',
        optionType: 'button',
      },
    },
    {
      fieldName: 'splitType',
      label: '划分类型',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.AI_DATASET_SPLIT_TYPE),
        placeholder: '请选择划分类型',
      },
    },
    {
      fieldName: 'qualityType',
      label: '质量类型',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.AI_DATASET_QUALITY_TYPE),
        placeholder: '请选择质量类型',
      },
    },
    {
      fieldName: 'auditStatus',
      label: '审核状态',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.INFRA_AUDIT_STATUS),
        placeholder: '请选择审核状态',
      },
    },
    {
      fieldName: 'auditNotes',
      label: '审核备注',
      component: 'Textarea',
      componentProps: {
        rows: 4,
        placeholder: '请输入审核备注',
      },
    },
    {
      fieldName: 'qualityScore',
      label: '质量评分',
      component: 'Rate',
    },
    {
      fieldName: 'source',
      label: '数据来源',
      component: 'Input',
      componentProps: {
        placeholder: '请输入数据来源，如：manual/auto/imported',
      },
    },
    {
      fieldName: 'auditType',
      label: '审核类型',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.AI_DATASET_AUDIT_TYPE),
        placeholder: '请选择审核类型',
      },
    },
  ];
}

/** 详情页字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      label: '数据集内容',
      field: 'rawContent',
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
    {
      label: '版本号',
      field: 'versionNum',
    },
    {
      label: '是否有效',
      field: 'isValid',
      render: (val) =>
        h(DictBadge, {
          value: val,
        }),
    },
    {
      label: '扩展字段',
      field: 'metadata',
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
    {
      label: '划分类型',
      field: 'splitType',
      render: (val) =>
        h(DictTag, {
          type: DICT_TYPE.AI_DATASET_SPLIT_TYPE,
          value: val,
        }),
    },
    {
      label: '质量类型',
      field: 'qualityType',
      render: (val) =>
        h(DictTag, {
          type: DICT_TYPE.AI_DATASET_QUALITY_TYPE,
          value: val,
        }),
    },
    {
      label: '审核状态',
      field: 'auditStatus',
      render: (val) =>
        h(DictTag, {
          type: DICT_TYPE.INFRA_AUDIT_STATUS,
          value: val,
        }),
    },
    {
      label: '审核备注',
      field: 'auditNotes',
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
    {
      label: '质量评分',
      field: 'qualityScore',
      render: (val) => h(Rate, { value: val, disabled: true }),
    },
    {
      label: '数据来源',
      field: 'source',
      render: (val) =>
        h(DictTag, {
          type: DICT_TYPE.AI_DATASET_SOURCE,
          value: val,
        }),
    },
    {
      label: '审核类型',
      field: 'auditType',
      render: (val) =>
        h(DictTag, {
          type: DICT_TYPE.AI_DATASET_AUDIT_TYPE,
          value: val,
        }),
    },
    {
      label: '上个版本id',
      field: 'previousId',
    },
    {
      label: '数据集id',
      field: 'datasetId',
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'datasetId',
      label: '数据集id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'versionId',
      label: '版本号',
      component: 'Select',
      dependencies: {
        triggerFields: ['datasetId'],
        async componentProps(values) {
          const list = await getVersionNumList(values.datasetId);
          const options = list.map((item) => ({
            value: item.id,
            label: item.versionNum,
            ...item,
          }));
          return {
            allowClear: false,
            options,
            placeholder: '请选择版本号',
          };
        },
      },
      rules: 'required',
    },
    {
      fieldName: 'rawContent',
      label: '样本内容',
      component: 'Input',
      componentProps: {
        placeholder: '模糊查询样本内容',
      },
    },
    {
      fieldName: 'isValid',
      label: '是否有效',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: [
          {
            label: '是',
            value: true,
          },
          {
            label: '否',
            value: false,
          },
        ],
        placeholder: '请输入是否有效',
      },
    },
    {
      fieldName: 'auditStatus',
      label: '审核状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.INFRA_AUDIT_STATUS),
        placeholder:
          '请选择审核状态：pending-待审核, approved-通过, rejected-不通过',
      },
    },
    {
      fieldName: 'source',
      label: '数据来源',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.AI_DATASET_SOURCE),
        placeholder: '请选择数据来源，如：manual/auto/imported',
      },
    },
    {
      fieldName: 'auditType',
      label: '审核类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.AI_DATASET_AUDIT_TYPE),
        placeholder: '请选择审核类型：llm-模型标注，manual-人工标准',
      },
    },
    {
      fieldName: 'splitType',
      label: '划分类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.AI_DATASET_SPLIT_TYPE),
        placeholder:
          '请选择划分类型:train-训练 eval-评估 test-测试 predict-预测',
      },
    },
    {
      fieldName: 'qualityType',
      label: '质量类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.AI_DATASET_QUALITY_TYPE),
        placeholder:
          '请选择质量类型:excellent-极好, good-好, fair-及格, poor-差',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<AiTrainingDatasetSamplesApi.TrainingDatasetSamples>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '数据集数据id',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'versionId',
      title: '数据集版本id',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'rawContent',
      title: '数据集内容',
      minWidth: 120,
    },
    {
      field: 'versionNum',
      title: '版本号',
      minWidth: 120,
    },
    {
      field: 'isValid',
      title: '是否有效',
      minWidth: 120,
      cellRender: {
        name: 'CellDictBadge',
      },
    },
    {
      field: 'metadata',
      title: '扩展字段',
      minWidth: 120,
    },
    {
      field: 'splitType',
      title: '划分类型',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.AI_DATASET_SPLIT_TYPE },
      },
    },
    {
      field: 'qualityType',
      title: '质量类型',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.AI_DATASET_QUALITY_TYPE },
      },
    },
    {
      field: 'auditStatus',
      title: '审核状态',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.INFRA_AUDIT_STATUS },
      },
    },
    {
      field: 'auditNotes',
      title: '审核备注',
      minWidth: 120,
    },
    {
      field: 'datasetId',
      title: '数据集id',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'qualityScore',
      title: '质量评分',
      minWidth: 120,
    },
    {
      field: 'source',
      title: '数据来源',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.AI_DATASET_SOURCE },
      },
    },
    {
      field: 'auditType',
      title: '审核类型',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.AI_DATASET_AUDIT_TYPE },
      },
    },
    {
      field: 'previousId',
      title: '上个版本id',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'actions',
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
